package com.ruantu.clothing.controller.report;

import com.ruantu.clothing.entity.JsonResponseMessage;
import com.ruantu.clothing.entity.PageBean;
import com.ruantu.clothing.entity.User;
import com.ruantu.clothing.entity.report.StoreReport;
import com.ruantu.clothing.service.report.StoreReportService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * Created by Administrator on 2018/8/10 0010.
 */
@Controller
@RequestMapping("/storereport")
public class StoreReportController {
    @Autowired
    private StoreReportService storeReportService;

    /**
     * 库存报表
     *
     * @param
     * @param
     * @param //session
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public JsonResponseMessage findStortReport(StoreReport stortReport, int fenlei, String sort, String order, String jgid, PageBean pageBean) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        jsonResponseMessage.setRows(storeReportService.findStoreReport(stortReport, fenlei, sort, order, jgid, pageBean));
        jsonResponseMessage.setTotal(pageBean.getTotal());
        jsonResponseMessage.setFooter(storeReportService.findStoreReportFooter(jgid, stortReport));
        return jsonResponseMessage;
    }


    /**
     * 导出报表
     *
     * @param
     * @param
     * @param //session
     * @return
     */
    @RequestMapping("/downloadexcel")
    @ResponseBody
    public void downExcel(ServletOutputStream out, HttpServletResponse response, StoreReport stortReport, int fenlei, String jgid) {
        try {
            XSSFWorkbook wb = storeReportService.downloadExcel(stortReport, jgid, fenlei);
            HttpHeaders headers = new HttpHeaders();
            ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
            String fileName = URLEncoder.encode("库存报表导出数据.xls", "utf-8");
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            wb.write(out);
            outByteStream.flush();
            outByteStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

}
